package com.ny.dao.impl;

import com.ny.dao.OrderDao;
import com.ny.entity.Order;
import com.ny.entity.Product;
import com.ny.until.SqlConnection;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderDaoImpl  implements OrderDao {
    Connection connection = SqlConnection.getCon();

    public OrderDaoImpl() throws SQLException {
    }

    @Override
    public List<Order> findAllOrder() throws SQLException {
        String sql = "select * from orders";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();

        List<Order> orderList = new ArrayList<Order>();
        while (resultSet.next()) {
            Order order = new Order();
            order.setOrder_id(resultSet.getInt("order_id"));
            order.setStudent_id(resultSet.getInt("student_id"));
            order.setProduct_id(resultSet.getInt("product_id"));
            order.setNumber(resultSet.getString("number"));
            order.setTotal_price(resultSet.getDouble("total_price"));
            orderList.add(order);
        }
        return orderList;
    }

    @Override
    public int addOrder(Order order) throws SQLException {
        String sql = "insert into orders(student_id,product_id,number,total_price) values(?,?,?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1, order.getStudent_id());
        preparedStatement.setInt(2, order.getProduct_id());
        preparedStatement.setString(3, order.getNumber());
        preparedStatement.setDouble(4, order.getTotal_price());
        int i = preparedStatement.executeUpdate();
        if (i > 0) {
            System.out.println("添加成功");
        }

        return 0;
    }

    @Override
    public int deleteOrder(int orderId) throws SQLException {
        String sql = "delete from orders where order_id = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1, orderId);
        int i = preparedStatement.executeUpdate();
        if (i > 0) {
            System.out.println("删除成功");
        }
        return 0;
    }

    @Override
    public int updateOrder(Order order) throws SQLException {
        String sql = "update orders set student_id = ?,product_id = ?,number = ?,total_price = ? where order_id = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1, order.getStudent_id());
        preparedStatement.setInt(2, order.getProduct_id());
        preparedStatement.setString(3, order.getNumber());
        preparedStatement.setDouble(4, order.getTotal_price());
        preparedStatement.setInt(5, order.getOrder_id());
        int i = preparedStatement.executeUpdate();
        if (i > 0) {
            System.out.println("修改成功");
        }
        return 0;
    }


}

